// pages/login/login.js
import Https from "../../utils/api"
const http = new Https
var interval = null //倒计时函数
Page({

  /**
   * 页面的初始数据
   */
  data: {
    time: '获取验证码', //倒计时 
    currentTime: 60,
    phone:0,
    code:0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
    
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  getCode: function (options){
    var that = this;
    var currentTime = that.data.currentTime
    interval = setInterval(function () {
      currentTime--;
      that.setData({
        time: currentTime+'秒'
      })
      if (currentTime <= 0) {
        clearInterval(interval)
        that.setData({
          time: '重新发送',
          currentTime:60,
          disabled: false   
        })
      }
    }, 1000)  
  },

  getphone(e){
    let phone = e.detail.value;
    this.setData({phone})
    console.log(this.data.phone);
  },
  getVerificationCode(){
    console.log(1)
    this.getCode();
    var that = this
    that.setData({
      disabled:true
    })
    http.request("send/sms",{phone:that.data.phone},"POST").then((res)=>{
      console.log(res)
      if(res.code == 200){
        wx.showToast({
          title: '发送成功',
          icon:"none"
        })
      }else{
        wx.showToast({
          title: res.message,
          icon:"error"
        })
      }
      
    })
  },

  code(e)
  {
    let code = e.detail.value;
    this.setData({code})
  },

  confirm_btn()
  {
    console.log(1)
    let phone = this.data.phone;
    let code = this.data.code;
    if(phone.length == 0 || code.length == 0){
      wx.showToast({
        title: "不允许为空",
        icon:"error"
      })
      return;
    }
    http.request("phone/login",{phone:this.data.phone,code:this.data.code},"POST").then((res)=>{
      console.log(res)
      if(res.code == 200){
        wx.setStorageSync('userInfo', res.data)
        wx.setStorageSync('hasUserInfo', true)
        wx.showToast({
          title: '登录成功',
          icon:"none",
        })
        if(res.data.is_first == 0){
          wx.navigateTo({
            url: '/pages/wxLogin/wxLogin',
          })
        }else{
          wx.navigateTo({
            url: '/pages/home/home',
          })
        }
        
      }else{
        wx.showToast({
          title: "登录失败",
          icon:"none",
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})